package com.ym.controller.patient.askDoctor;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.core.utils.tkmybatis.TKCriteria;
import com.ym.medical.domain.entity.CustomTaglibraryEntity;
import com.ym.medical.domain.entity.StandarTaglibraryEntity;
import com.ym.medical.domain.param.StandarTagLibraryParam;
import com.ym.medical.service.ICustomTaglibrarySV;
import com.ym.medical.service.IStandarTaglibrarySV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author dai
 * @create 2019-05-07 11:32
 **/
@Api(tags = "患者端-首页-标签库相关")
@RestController
@ApiSign
@RequestMapping(value = "/patient/ask/tag",headers= Constants.Api.VERSION)
public class TagLibraryController extends BaseController {
	private static final Logger LOGGER = LoggerFactory.getLogger(TagLibraryController.class);

	@Autowired
	private IStandarTaglibrarySV standarTaglibrarySV;

	@Autowired
	private ICustomTaglibrarySV customTaglibrarySV;

	@ApiOperation("标准标签展示")
	@GetMapping(value = "/list")
	public APIResponse<PageInfo<StandarTaglibraryEntity>> listPage(StandarTagLibraryParam standarTaglibraryModel, @RequestParam(defaultValue = "1", required = false) int pageNum, @RequestParam(defaultValue = "5", required = false) int pageSize) {
		TK<StandarTaglibraryEntity> select = TK.select(StandarTaglibraryEntity.class);
		TKCriteria<StandarTaglibraryEntity, Object> where = select.where();
		Integer num = standarTaglibraryModel.getNum();
		if (num != null) {
			if (num == 1) {
				where.andEqualTo(StandarTaglibraryEntity::getTglbLevel,"01");
			} else if (num == 2 || num == 3) {
				where.andIn(StandarTaglibraryEntity::getTglbLevel,new String[]{"02","03"});
			} else if (num == 4 || num == 5) {
				where.andIn(StandarTaglibraryEntity::getTglbLevel,new String[]{"04","05"});
			}
		}
		PageHelper.startPage(pageNum,pageSize);
		List<StandarTaglibraryEntity> standarTaglibraryEntities = standarTaglibrarySV.selectList(select);
		return successPage(standarTaglibraryEntities);
	}

	@Log
	@ApiOperation("自定义标签增加")
	@PostMapping(value = "/insert")
	public APIResponse<CustomTaglibraryEntity> insertTag(@RequestBody CustomTaglibraryEntity customTaglibrary) {
		customTaglibrarySV.insert(customTaglibrary);
		return successData(customTaglibrary);

	}

	@Log
	@ApiOperation("自定义标签删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "自定义标签id", required = true, paramType = "query", dataType = "Long"),
	})
	@GetMapping(value = "/delete")
	public APIResponse deleteTag(@RequestParam Long id) {
		customTaglibrarySV.deleteById(id);
		return successData(id);
	}
}
